﻿using NfsBLL;
using NfsCommon.Util;
using NfsModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ManagerWed.Controllers
{
    public class LevelController : Controller
    {
        //
        // GET: /Level/
        public ActionResult Index()
        {
            ViewData["hiden_type"] = CookieHelper.GetDecodeCookie("pcnfs_type");

            return View();
        }
        #region PC模糊查询
        public JsonResult query_level(int pageindex, int pagesize)
        {
            try
            {
                LevelBLL bll = new LevelBLL();
                List<level> list = bll.GetLevelBycondtion(pageindex, pagesize);
                object pagecount = bll.GetLevelBycondtionCount();
                return Json(new { MessageCode = "0", LevelList = list, Pagecount = (int.Parse(pagecount.ToString()) + pagesize - 1) / pagesize }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception)
            {

                return Json(new { MessageCode = "200" }, JsonRequestBehavior.AllowGet);

            }

        }
        #endregion

        #region 新增和修改数据

        public JsonResult GetLevel(int id)
        {
            LevelBLL bll = new LevelBLL();
            level model = bll.GetModel(id);
            return Json(new { model = model }, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public JsonResult OpeaLevels()
        {
            try
            {  
                LevelBLL bll = new LevelBLL();
                int id = Request["id"] != null ? int.Parse(Request["id"].ToString()) : 0;
                string opea = Request["opea"] != null ? Request["opea"] : "";
                int jibie = Request["jibie"] != null ?int.Parse( Request["jibie"]) :0;
                string coding = Request["condig"] != null ? Request["condig"] : "";               
                string Name = Request["name"] != null ? Request["name"] : "";
                string code=Request["code"] != null ? Request["code"] : "";
                string description = Request["description"] != null ? Request["description"] : "";
                if (!bll.GetSingle(id, Name, coding))
                {
                    level model = new level { id = id, parent_id=jibie,coding=coding, name = Name, description = description, valid = 1, time = DateTime.Now };
                    bool flag;
                    if (opea == "add")
                    {
                        int ids;
                        flag = bll.Add(model,out ids);
                        if (flag){ 
                          int Maxid = bll.GetMaxId()-1;
                        if (jibie.ToString() != "0"){
                          string Lcode = code + "00" + Maxid.ToString();
                            bll.UptLevel_code(Maxid,Lcode);
                          }else{
                            string Lcode = "00" + Maxid.ToString();
                            bll.UptLevel_code(Maxid, Lcode);
                           }
                        }
                    }
                    else
                    {
                        flag = bll.Update(model);
                    }
                    if (flag)
                    {
                        return Json(new { MessageCode = "0" }, JsonRequestBehavior.AllowGet);
                    }
                    else
                    {
                        return Json(new { MessageCode = "100" }, JsonRequestBehavior.AllowGet);
                    }
                }
                else {

                    return Json(new { MessageCode = "50" }, JsonRequestBehavior.AllowGet);
                }
               
            }
            catch (Exception)
            {

                return Json(new { MessageCode = "200" }, JsonRequestBehavior.AllowGet);

            }


        }
        [HttpPost]
        public JsonResult OpeaLevel()
        {
            try
            {
                LevelBLL bll = new LevelBLL();
                int id = Request["id"] != null ? int.Parse(Request["id"].ToString()) : 0;
                string opea = Request["opea"] != null ? Request["opea"] : "";
                int jibie = Request["jibie"] != null ? int.Parse(Request["jibie"]) : 0;
                string coding = Request["condig"] != null ? Request["condig"] : "";
                string Name = Request["name"] != null ? Request["name"] : "";
                string code = Request["code"] != null ? Request["code"] : "";
                string description = Request["description"] != null ? Request["description"] : "";
                if (!bll.GetSingle(id, Name, coding))
                {
                    level model = new level { id = id, parent_id = jibie, coding = coding, name = Name, description = description, valid = 1, time = DateTime.Now };
                    bool flag;
                    if (opea == "add")
                    {
                        flag = bll.InsertLevel(model, code);
                    }
                    else
                    {
                        flag = bll.updateLevel(model,code);
                    }
                    if (flag)
                    {
                        return Json(new { MessageCode = "0" }, JsonRequestBehavior.AllowGet);
                    }
                    else
                    {
                        return Json(new { MessageCode = "100" }, JsonRequestBehavior.AllowGet);
                    }
                }
                else
                {

                    return Json(new { MessageCode = "50" }, JsonRequestBehavior.AllowGet);
                }

            }
            catch (Exception)
            {

                return Json(new { MessageCode = "200" }, JsonRequestBehavior.AllowGet);

            }


        }

        #endregion

        #region 绑定下拉框类别
        public JsonResult GetLevelName()
        {
            try
            {
                LevelBLL bll = new LevelBLL();
                List<level> list = bll.GetModelList(" valid=1");
                return Json(new { MessageCode = "0", list = list }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception)
            {
                return Json(new { MessageCode = "200" }, JsonRequestBehavior.AllowGet);
            }

        }
        #endregion

        #region 删除数据
        [HttpPost]
        public JsonResult delete()
        {
            try
            {
                AccountBLL bll = new AccountBLL();
                int id = Request["id"] != "" ? int.Parse(Request["id"]) : 0;
                if (bll.com_delelte(id, "t_level", "valid", 0))
                {
                    return Json(new { MessageCode = "0" }, JsonRequestBehavior.AllowGet);
                }
                else
                {

                    return Json(new { MessageCode = "100" }, JsonRequestBehavior.AllowGet);
                }

            }
            catch (Exception)
            {

                return Json(new { MessageCode = "200" }, JsonRequestBehavior.AllowGet);
            }


        }
        #endregion
    }
}